package com.admin.service;

import com.admin.util.DatabaseUtil;
import com.admin.util.Result;
import com.data.entity.Database;
import com.data.mapper.DatabaseMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Configuration
public class DatabaseService  {
    @Resource
    private DatabaseMapper databaseMapper;
    @Value("${databackup.address}")
    private String address;
    @Value("${databackup.username}")
    private String username;
    @Value("${databackup.password}")
    private String password;
    @Value("${databackup.database-name}")
    private String databaseName;

    public List<Database> listPage(Integer page, Integer limit) {
        if(page>0){
            page=(page-1)*limit;
        }
        return databaseMapper.listPage(page,limit);
    }

    public int count() {
        return databaseMapper.count();
    }

    public Database getInfo(Long id) {
        return databaseMapper.selectById(id);
    }

    public Result backup() {
        Result result = new Result();
        try {
            String cmd = "mysqldump -h"+address+" -u"+username+" -p"+password+"  "+ databaseName;
//            String command = "mysqldump -h8.134.52.142 -uroot -pMsdw@mysql123 smart-finance";//参数依次是IP、账号、密码、数据库名
            String directory = "D:/smart-finance/sqlData/";
            String fileName = "smart-finance"+new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".sql";
            String savePath1 = directory+fileName;
            DatabaseUtil.createDir(directory);
            boolean b1 =DatabaseUtil.backup(cmd, savePath1);
            if (b1){
                Database database = new Database();
                database.setDirectory(directory);
                database.setFileName(fileName);
                databaseMapper.insert(database);
                result.setMessage(Result.Code.SUCCESS,"数据备份成功");
                result.setData(database);
            }else {
                result.setMessage(Result.Code.FAILED,"数据备份失败");
            }
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
        }
        return result;
    }

    public int delete(Long id) {
        return databaseMapper.deleteById(id);
    }

    public Result rollback(Long id) {
        Result result=new Result();
        try {
            //测试还原
//            String command2 = "mysql -h8.134.52.142 -uroot -pMsdw@mysql123 --default-character-set=utf8 smart-finance";
            String cmd = "mysqldump -h"+address+" -u"+username+" -p"+password+" --default-character-set=utf8  "+ databaseName;
            Database database = databaseMapper.selectById(id);
            String fileName = database.getDirectory()+database.getFileName();
            boolean b2 = DatabaseUtil.recover(cmd, fileName);
            if(b2){
                result.setMessage(Result.Code.SUCCESS,"还原数据库成功");
            }else {
                result.setMessage(Result.Code.FAILED,"还原数据库失败");
            }
        }catch (Exception e){
            result.setMessage(Result.Code.SYSTEMERROR,"还原数据库失败");
        }
        return result;
    }
}
